package yfkj.gz.task.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import yfkj.gz.task.entity.SysRole;
import yfkj.gz.task.entity.SysUser;
import yfkj.gz.task.service.ISysRoleService;
import yfkj.gz.task.service.ISysUserService;
import yfkj.gz.task.util.DateUtil;
import yfkj.gz.task.util.StringUtils;
import yfkj.gz.support.BTView;
import yfkj.gz.support.controller.BaseController;

/**
 * 用户控制器
 * @author 胡汉三
 * @date   2017年1月16日 下午2:31:39
 */
@Controller
@RequestMapping("/sysuser")
public class SysUserController extends BaseController{

	@Resource
	private ISysUserService userService;
	@Resource
	private ISysRoleService roleService;

	/**
	 * 分页查询用户
	 * @param response
	 * @param user
	 * @param btView
	 * @throws IOException 
	 */
	@RequestMapping(value = "/findUser", method = { RequestMethod.POST, RequestMethod.GET })
	public void findUser(SysUser user,BTView<SysUser> btView) throws IOException{
		List<SysUser> list = userService.findSysUserPage(btView, null);
		btView.setRows(list);
		super.writeJSON(btView);
	}

	/**
	 * 用户登录
	 * @param response
	 * @param user
	 * @throws IOException
	 */
	@RequestMapping(value = "/login", method = { RequestMethod.POST, RequestMethod.GET })
	public void login(SysUser user,boolean rememberMe) throws IOException{
		result.setSuccess(false);
		//用户登录
		SysUser userInfo = userService.getByProerties(new String[]{"loginAccount"}, new String[]{user.getLoginAccount()},null);
		if(userInfo==null){
			result.setMessage("用户名错误");
			super.writeJSON(result);
			return;
		}
		if(!userInfo.getLoginPass().equals(new Sha256Hash(user.getLoginPass()).toHex())){
			result.setMessage("密码错误");
			super.writeJSON(result);
			return;
		}
		//存入session
		Subject subject = SecurityUtils.getSubject();
		//记得传入明文密码
		subject.login(new UsernamePasswordToken(userInfo.getLoginAccount(), user.getLoginPass(), rememberMe));
		session.setAttribute(USER_SESSION, userInfo);
		result.setMessage("登录成功");
		result.setSuccess(true);
		super.writeJSON(result);
	}

	/**
	 * 用户注册
	 * @param response
	 * @param user
	 * @throws IOException
	 */
	@RequestMapping(value = "/register", method = { RequestMethod.POST, RequestMethod.GET })
	public void register(SysUser user) throws IOException{
		result.setSuccess(false);
		Long count = userService.getCountByProerties(new String[]{"loginAccount"}, new String[]{user.getLoginAccount()});
		if(count>0){
			result.setMessage("账号已存在");
			super.writeJSON(result);
			return;
		}
		Long countEmail = userService.getCountByProerties(new String[]{"userEmail"}, new String[]{user.getUserEmail()});
		if(countEmail>0){
			result.setMessage("邮箱已存在");
			super.writeJSON(result);
			return;
		}
		try{
			//注册时间
			user.setRegisterTime(DateUtil.getDateTime(new Date()));
			//Sha256Hash加密
			user.setLoginPass(new Sha256Hash(user.getLoginPass()).toHex());
			//默认为注册用户
			SysRole role = roleService.getByProerties(new String[]{"roleKey"},new String[]{"ROLE_USER"},null);
			user.getRoles().add(role);
			userService.save(user);
			//存入session
			Subject subject = SecurityUtils.getSubject();
			subject.login(new UsernamePasswordToken(user.getLoginAccount(), user.getLoginPass()));
			session.setAttribute(USER_SESSION, user);
			result.setMessage("注册成功");
			result.setSuccess(true);
		}catch(Exception e){
			result.setMessage("注册失败");
		}
		super.writeJSON(result);
	}


	/**
	 * 判断用户账号是否已存在
	 * @param response
	 * @param user
	 * @throws IOException
	 */
	@RequestMapping(value = "/getUserNameCount", method = { RequestMethod.POST, RequestMethod.GET })
	public void getUserNameCount(String loginAccount) throws IOException{
		result.setSuccess(false);
		if(StringUtils.isBlank(loginAccount)){
			result.setMessage("账号不能为空");
			super.writeJSON(result);
			return;
		}
		Long count = userService.getCountByProerties(new String[]{"loginAccount"}, new String[]{loginAccount});
		if(count>0){
			result.setMessage("账号已存在");
		}else{
			result.setSuccess(true);
			result.setMessage("该账号可用");
		}
		super.writeJSON(result);
	}
	
	/**
	 * 判断用户邮箱是否已存在
	 * @param response
	 * @param email
	 * @throws IOException
	 */
	@RequestMapping(value = "/getEMailCount", method = { RequestMethod.POST, RequestMethod.GET })
	public void getEMailCount(String email) throws IOException{
		result.setSuccess(false);
		if(StringUtils.isBlank(email)){
			result.setMessage("邮箱不能为空");
			super.writeJSON(result);
			return;
		}
		Long count = userService.getCountByProerties(new String[]{"userEmail"}, new String[]{email});
		if(count>0){
			result.setMessage("邮箱已存在");
		}else{
			result.setSuccess(true);
			result.setMessage("该邮箱可用");
		}
		super.writeJSON(result);
	}

	// 登出
	@RequestMapping("/logout")
	public void logout() throws IOException {
		//退出权限验证
		SecurityUtils.getSubject().logout();
		//销毁session
		session.invalidate(); 
		response.sendRedirect(request.getContextPath()+"/login.jsp");
	}
}
